package com.scau.XFourPOS.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.opensymphony.xwork2.ActionSupport;

public class BaseAction extends ActionSupport {
	protected int page = 0;
	protected int rows = 10;
	protected String sort;
	protected String order;
	protected int id;

	public void writeJson(long total, List list) {
		try {
			Map<String, Object> maps = new HashMap<String, Object>();
			maps.put("rows", list);
			maps.put("total", total);
			
			String json = JSON.toJSONString(maps);
			
			json = JSONObject.toJSONString(maps,SerializerFeature.DisableCircularReferenceDetect);
			System.out.println(json);
			ServletActionContext.getResponse().setContentType(
					"text/html;charset=utf-8");
			ServletActionContext.getResponse().getWriter().write(json);
			ServletActionContext.getResponse().getWriter().flush();
		} catch (IOException e) {
		}
	}

	public void writeJson(Object object) {
		try {
			String json = JSON.toJSONString(object);
			ServletActionContext.getResponse().setContentType(
					"text/html;charset=utf-8");
			ServletActionContext.getResponse().getWriter().write(json);
			ServletActionContext.getResponse().getWriter().flush();
		} catch (IOException e) {
		}
		// 此处省略了setter和getter
	}
	public void writeJson(String string) {
		try {
			ServletActionContext.getResponse().setContentType(
					"text/html;charset=utf-8");
			ServletActionContext.getResponse().getWriter().write(string);
			ServletActionContext.getResponse().getWriter().flush();
		} catch (IOException e) {
		}
		// 此处省略了setter和getter
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page - 1;
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public String getSort() {
		return sort;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public String getOrder() {
		return order;
	}

	public void setOrder(String order) {
		this.order = order;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}
}